package com.jf.docsify.modules.system.user;

import com.jf.docsify.common.exception.DocsifyException;
import com.jf.docsify.common.utils.Resp;
import com.jf.docsify.config.DocsifyConfig;
import com.jf.docsify.modules.model.SysAuthRegister;
import com.jf.docsify.modules.model.SysUser;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;

/**
 * Description:
 *
 * @author chenwr
 * @version 1.0
 *
 * <pre>
 * 修改记录:
 * 修改后版本           修改人       修改日期         修改内容
 * 2022/8/2.1       chenwr    2022/8/2           Create
 * </pre>
 * @date 2022/8/2
 */
@Path("/auth/register")
public class AuthRegisterController extends Controller {

    @Inject
    private AuthRegisterService authRegisterService;

    public void list() {
        String pageSize = getPara("pageSize");
        String currentPage = getPara("currentPage");
        Page<SysAuthRegister> list = authRegisterService.list(currentPage, pageSize);
        Resp.success(list).done(this);
    }

    public void delete() {
        String id = getPara(0);
        boolean delete = authRegisterService.deleteById(id);
        Resp.resp(delete).done(this);
    }

    public void add() {
        String rawData = getRawData();
        SysAuthRegister authRegister = JsonKit.parse(rawData, SysAuthRegister.class);
        String userName = authRegister.getUserName();
        String superAdminUserName = DocsifyConfig.superAdminUserName;
        SysUser user = getAttr("user");
        String currentLoginUserName = user.getUserName();
        if (!currentLoginUserName.equals(superAdminUserName)) {
            DocsifyException.throwE("无操作权限");
        }
        SysAuthRegister sysAuthRegister = new SysAuthRegister();
        sysAuthRegister.setUserName(userName);
        sysAuthRegister.setExpireTime(authRegister.getExpireTime());
        boolean saveFlag = authRegisterService.addAuthRegisterUser(sysAuthRegister);
        Resp.resp(saveFlag).done(this);
    }
}